package cjq.demo.push;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;


/**
 * 推送工具类，提供群推和单推的api
 * @author zzhan@linewell.com
 * @since 2014-05-08
 */
public class PushUtils {

	public static final String SEND_ALL = "sendAll";
	public static final String SEND_SINGLE = "sendSingle";
	
	/**
	 * 群推
	 * @param bean 推送的bean对象
	 * @throws Exception
	 */
	public static void sendAll(PushBean bean) throws Exception{
		
		String pushUrl = "http://127.0.0.1:7070/Androidpn/service/notification?wsdl";
		
		if(null == pushUrl) throw new RuntimeException("服务地址不能为空!请配置服务地址");
		
		String title = bean.getTitle();
		String message = bean.getMessage();
		String uri = bean.getUri();
		
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
				.newInstance();
		Client client = clientFactory.createClient(pushUrl);
		client.invoke(SEND_ALL, title,message,uri);
	}
	
	/**
	 * 单推
	 * @param bean 推送的Bean对象，其中username属性不能为空
	 * @throws Exception
	 */
	public static void sendSingle(PushBean bean) throws Exception{
		
		String pushUrl = "http://127.0.0.1:7070/Androidpn/service/notification?wsdl";
		if(null == pushUrl) throw new RuntimeException("服务地址不能为空!请配置服务地址");
		String title = bean.getTitle();
		String message = bean.getMessage();
		String uri = bean.getUri();
		String username = bean.getUsername();
		
		if(null == username) throw new RuntimeException("请设置PushBean的username属性");
		
		JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory
				.newInstance();
		Client client = clientFactory.createClient(pushUrl);
		client.invoke(SEND_SINGLE, title,message,uri,username);
		
	}
}
